package com.jhx.advertisingscreen;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.lake.banner.transformer.VerticalPageTransformer;
import com.lake.banner.view.BannerViewPager;
import com.lake.hbanner.HBanner;
import com.lake.hbanner.ImageSubView;
import com.lake.hbanner.SubView;
import com.lake.hbanner.VideoSubView;
import com.lake.hbanner.VideoViewType;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean.DataUrlBea> lists = new ArrayList<>();
    private List<SubView> sublist = new ArrayList<>();
    private BannerViewPager bannerViewPager;
    private HBanner hBanner;


    private Handler mHandler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            hBanner = HBanner.create(bannerViewPager);
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i).getType().equals("1")) {
                    sublist.add(new ImageSubView.Builder(getBaseContext())
                            .url(lists.get(i).getUrl())
                            //.gravity(ImageView.ScaleType.FIT_XY)
                            .build());

                } else {
                    sublist.add(new VideoSubView.Builder(getBaseContext())
                            .url(lists.get(i).getUrl())
                            .gravity(VideoViewType.FULL)
                            .playOffset(300)
                            .isSub(false)
                            .build());
                }
            }
            LogUtils.e(sublist.size() + sublist.toString());
            hBanner.sources(sublist);
            hBanner.play(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ImmersionBar.with(this)
                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                .init();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bannerViewPager = findViewById(R.id.bannerViewPager);
        bannerViewPager.setPageTransformer(true, new VerticalPageTransformer());
        bannerViewPager.setScrollable(false);
        bannerViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        OkGo.<String>get("http://47.97.20.225/prod-api/endport/config/100")
                .execute(new JsonCallback<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            Bean bean = JSON.parseObject(response.body(), Bean.class);
                            LogUtils.e(bean.getData().getEndportResource());
                            lists = JSON.parseArray(bean.getData().getEndportResource(), Bean.DataUrlBea.class);
                            mHandler.post(runnable);
                        } else {
                            LogUtils.e(response.code());
                        }
                    }
                });


    }
}